/**
 * 
 */
package com.wbcs.jbsf.modal.abs;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 类似Map<String, Map<String, AbsRowModal>>或者Map<String, List<AbsRowModal>>模式的模型对象接口
 * @author hawkfly
 */
public abstract class AbsTableModal
{
    private Log log = LogFactory.getLog(AbsTableModal.class);
    private String tablename;
    private String keyname;
    private List<AbsRowModal> ldata;//行集合
    private Map<Object, AbsRowModal> mdata;//用唯一键标示的行对象
    
    public AbsTableModal(String tablename){
        this.tablename = tablename;
        ldata = new ArrayList<AbsRowModal>();
    }
    
    public AbsTableModal(String tablename, String keyname){
        this.tablename = tablename;
        this.keyname = keyname;
        mdata = new LinkedHashMap<Object, AbsRowModal>();
    }
    
    public AbsTableModal addRow(AbsRowModal row){
        if(keyname == null){
            if(ldata == null)ldata = new ArrayList<AbsRowModal>();
            ldata.add(row);
        }else{
            if(mdata == null)mdata = new LinkedHashMap<Object, AbsRowModal>();
            mdata.put(row.getColValue(keyname),row);
        }
        return this;
    }
    
    public AbsTableModal addRows(Map<Object,AbsRowModal> rows){
        if(mdata == null)mdata = new LinkedHashMap<Object, AbsRowModal>();
        mdata.putAll(rows);
        return this;
    }
    
    public AbsRowModal getRow(int rownum){
        return ldata.get(rownum);
    }
    
    public AbsRowModal getRow(Object rowkey){
        AbsRowModal rtn = null;
        try{
            if(mdata == null)throw new Exception("未正确使用tablemodal的map形式");
            rtn = mdata.get(rowkey);
        }catch(Exception e){
            log.error("未正确使用tablemodal的map形式");
            e.printStackTrace();
        }
        return rtn;
    }
    
    public AbsRowModal newRow(){
        AbsRowModal newrow = new AbsRowModal(){};
        addRow(newrow);
        return newrow;
    }
    
    /**
     * 
     * @param where name='aaa'
     * @return
     */
    public AbsRowModal[] findRowByWhere(String where){
        List<AbsRowModal> row = new ArrayList<AbsRowModal>();
        //对where条件进行分析,将所有条件装换为map形式
        //对逻辑关系分析（and, or）,对应出&&和||
        //最终的map像这样，{{colname1, colvalue},{唯一值, and},{colname1,colvalueAry}}
        for(int i=0;i<ldata.size();i++)
        {
            
        }
        //需要根据需要清空重组AbsRowModal
        return null;
    }

    public String getTablename()
    {
        return tablename;
    }
    
    public  List<AbsRowModal> getAllstDatas(){
        return ldata;
    }
    
    public List<Map<String, Object>> getAllstDatas2Collection(){
        List<Map<String, Object>> lst = new ArrayList<Map<String, Object>>();
        for(int i=0;i<ldata.size();i++)
        {
            lst.add(ldata.get(i).toMap());
        }
        return lst;
    }
    
    public Map<Object, AbsRowModal> getAllMDatas(){
        return mdata;
    }
    
    public Map<Object, Map<String, Object>> getAllMDatas2Collection(){
        Map<Object, Map<String, Object>> map = new LinkedHashMap<Object, Map<String, Object>>();
        Iterator<Object> it = mdata.keySet().iterator();
        while(it.hasNext()){
            Object key = it.next();
            map.put(key,mdata.get(key).toMap());
        }
        return map;
    }
    
    /**
     * 按照指定的有效key,将原来的数据集合进行key值置换
     * @param key
     * @return
     */
    public AbsTableSpaceModal parseLst2Maplst(String key){
        AbsTableSpaceModal tsmodal = new AbsTableSpaceModal(){};
        for(int i=0;i<ldata.size();i++)
        {
            Object value = ldata.get(i).getColValue(key);
            String valuestr = String.valueOf(value == null ? "" : value);
            AbsTableModal tableModal = tsmodal.getTable(valuestr);
            if(tableModal == null){//新的未分类菜单
                tableModal = tsmodal.newTable(valuestr);
            }
            tableModal.addRow(ldata.get(i));
        }
        return tsmodal;
    }
}

